package com.asiainfo.complier.model;

import java.awt.Color;
import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

import com.asiainfo.complier.common.OutLineInfo;
import com.asiainfo.complier.helper.UIHelper;

public class NameAndPictureListCellRenderer extends JLabel implements
		TreeCellRenderer /* ListCellRenderer */{
	/**
	 * NameAndPictureListCellRenderer
	 */
	private static final long serialVersionUID = -3796126434706674678L;
	// private Border lineBorder = BorderFactory.createLineBorder(Color.red,
	// 2),emptyBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);

	private ImageIcon classIco, methodIco, packetIco, mainIco, importIco;

	{
		classIco = UIHelper.getImageIco("com/asiainfo/images/class.png");
		methodIco = UIHelper.getImageIco("com/asiainfo/images/method.png");
		packetIco = UIHelper.getImageIco("com/asiainfo/images/dir.png");
		mainIco = UIHelper.getImageIco("com/asiainfo/images/archive.png");
		importIco = UIHelper.getImageIco("com/asiainfo/images/dot.png");

	}

	public NameAndPictureListCellRenderer() {
		setOpaque(true);

	}

	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean isSelected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

		if (node.getUserObject() instanceof OutLineInfo) {
			OutLineInfo info = (OutLineInfo) node.getUserObject();
			if (info != null) {
				setText(info.toString());
				if (info.getOutLineType() == 0) {
					setIcon(mainIco);
				} else if (info.getOutLineType() == 1) {
					setIcon(packetIco);
				} else if (info.getOutLineType() == 2) {
					setIcon(classIco);
				} else if (info.getOutLineType() == 4) {
					setIcon(importIco);
				} else {
					if (info.getReturnType() != null
							&& info.getReturnType().indexOf("void") == -1) {
						setIcon(classIco);
					} else {
						setIcon(methodIco);
					}
				}

				if (isSelected) {
					setForeground(Color.red);
					setBackground(Color.green);
				} else {
					setForeground(tree.getForeground());
					setBackground(tree.getBackground());
				}

			}
		}

		return this;
	}
}
